package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.main1.OrderConfirmActivity;
import com.payment.ktb.activity.main1.OrderConfirmBuyMembershipActivity;
import com.payment.ktb.constants.ConstantsPay;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/29.
 */

public class AgentOrderActivity extends BaseActivity {

    @BindView(R.id.iv_agentorder_medal1)
    ImageView iv_agentorder_medal1;
    @BindView(R.id.iv_agentorder_medal2)
    ImageView iv_agentorder_medal2;
    @BindView(R.id.iv_agentorder_medal3)
    ImageView iv_agentorder_medal3;
    @BindView(R.id.iv_agentorder_medal4)
    ImageView iv_agentorder_medal4;
    @BindView(R.id.tv_agentorder_amount)
    TextView tv_agentorder_amount;
    @BindView(R.id.tv_agentorder_agentlevel)
    TextView tv_agentorder_agentlevel;
    @BindView(R.id.btn_agentorder_pay)
    Button btn_agentorder_pay;
    private String linkId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agentorder);
        ButterKnife.bind(this);
        initTitle("代理商");
        initData();

    }

    private void initData() {

        //L33 经销商 *
        //L31 一级代理 **
        //L30 办事处 ***
        //其它 ****
        String orgType = getIntent().getStringExtra("orgType");
        if (orgType.equals("L33")) {
            iv_agentorder_medal2.setVisibility(View.GONE);
            iv_agentorder_medal3.setVisibility(View.GONE);
            iv_agentorder_medal4.setVisibility(View.GONE);
            tv_agentorder_agentlevel.setText("恭喜您成为经销商级别代理商！");
        }else if (orgType.equals("L31")) {
            iv_agentorder_medal3.setVisibility(View.GONE);
            iv_agentorder_medal4.setVisibility(View.GONE);
            tv_agentorder_agentlevel.setText("恭喜您成为一级代理级别代理商！");

        }else if (orgType.equals("L30")) {
            iv_agentorder_medal4.setVisibility(View.GONE);
            tv_agentorder_agentlevel.setText("恭喜您成为办事处级别代理商！");

        }else {
            tv_agentorder_agentlevel.setText("恭喜您成为内部员工级别代理商！");

        }

        tv_agentorder_amount.setText(handleFen2Yuan(getIntent().getStringExtra("payPrice")));
    }

    private String handleFen2Yuan(String fen) {
        if (fen.equals("0")) {
            return "0.00";
        }
        double srcFen = Double.valueOf(fen);
        double roundSrcFen = Math.round(srcFen);
        return String.valueOf(roundSrcFen/100);
    }


    @OnClick({R.id.btn_agentorder_pay})
    public void clickEvent(View view) {

        switch (view.getId()) {

            //去支付
            case R.id.btn_agentorder_pay:

                createOrder();

                break;


        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        linkId =  String.valueOf(new Date().getTime());
    }

    private void createOrder() {

        progressDialogUtils.showProgress();

        //构造传输参数
        final Map<String, String> map = new HashMap<String,String>();
        map.put("linkId",linkId);
        map.put("payType","1");
        map.put("orderType","10");
        map.put("amount",Double.valueOf(getIntent().getStringExtra("payPrice")) + "");
        map.put("goodsName",getIntent().getStringExtra("taskId"));
        map.put("product", ConstantsPay.product1002);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.PayOrderCreate, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Intent intent = new Intent(mContext,OrderConfirmBuyMembershipActivity.class);
                    intent.putExtra("amount",getIntent().getStringExtra("payPrice"));
                    intent.putExtra("bindCards",jsonObject.optString("bindCards"));
                    intent.putExtra("orderNo",jsonObject.optString("orderNo"));
                    intent.putExtra("srcActivity","AgentOrderActivity");
                    startActivity(intent);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });


    }




}
